/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    typedef TreeNode node;
    node *ans;
    void dfs(node *u, int &k)
    {
        if(!u) return;
        dfs(u->left, k); //倒数第k个节点，就先右后左
        if(k==0) return;
        if(--k==0) {ans=u; return;}
        dfs(u->right, k);
    }
    
    TreeNode* kthNode(TreeNode* root, int k) {
        dfs(root, k);
        return ans;
    }
};